module utils {
	export class ScreenManager {
		private static _instance: utils.ScreenManager;
		private _baseContainer: egret.DisplayObjectContainer;
		private _mainContainer:egret.DisplayObjectContainer;
		private shelter:egret.Shape;
		private _curScreen:BaseScreen;
		private prevScreen:BaseScreen;
		private showScreen:BaseScreen;
		private _main_screen:MainScreen;
		public constructor() {
			this.init();
		}

		private init():void
		{
			this.shelter=new egret.Shape();
			this.shelter.graphics.beginFill(0xacacac);
			this.shelter.graphics.drawRect(0,0,CoreData.instance.screenW,CoreData.instance.screenH);
			this.shelter.graphics.endFill();
			// this.shelter.cacheAsBitmap=true;
		}

		public static get instance(): utils.ScreenManager {
			if (!utils.ScreenManager._instance) {
				this._instance = new utils.ScreenManager();
			}
			return this._instance;
		}

		public get main_screen():MainScreen {
			return this._main_screen;
		}

		public set main_screen(value: MainScreen) {
			this._main_screen = value;
		}

		public get MainContainer():egret.DisplayObjectContainer {
			return this._mainContainer;
		}

		public set MainContainer(value: egret.DisplayObjectContainer) {
			this._mainContainer = value;
		}


		public get baseContainer():egret.DisplayObjectContainer {
			return this._baseContainer;
		}

		public set baseContainer(value: egret.DisplayObjectContainer) {
			this._baseContainer = value;
		}

		//转换到展示页面
		public changeToShowScreen(screenNum:number):void
		{
			if(screenNum == 1){
				this.showScreen=<BaseScreen>this.getScreenByName("MainScreen",null);
			}
			else{
				this.showScreen=<BaseScreen>this.getScreenByName("gpage"+screenNum,null);
			}
			
			this.shelter.alpha=0;
			utils.CoreData.instance.stage.addChild(this.shelter);
			// TweenMax.to(this.shelter,.2,{alpha:1,ease:Cubic.easeIn,onComplete:()=>{
			// 	this._baseContainer.removeChild(this._curScreen);
			// 	this.prevScreen=this._curScreen;
			// 	this.curScreen=this.showScreen;
			// 	this._baseContainer.addChild(this._curScreen);
			// 	TweenMax.to(this.shelter,.65,{alpha:0,ease:Sine.easeOut,onComplete:()=>{
			// 		this.shelter.parent.removeChild(this.shelter);
			// 	}});
			// }});

			egret.Tween.get(this.shelter).to({'alpha':1},200).call(function(){
				this._baseContainer.removeChild(this._curScreen);
				this.prevScreen=this._curScreen;
				this.curScreen=this.showScreen;
				this._baseContainer.addChild(this._curScreen);

				egret.Tween.get(this.shelter).to({'alpha':0},650).call(function(){
					this.shelter.parent.removeChild(this.shelter);
				}, this);
			},this);

		}
		//从展示场景回退到主场景
		public showBackToMain():void
		{
			if(!this.prevScreen) return;
			this._baseContainer.addChild(this.prevScreen);
			this._baseContainer.addChild(this._curScreen);
			// TweenMax.to(this._curScreen,.4,{alpha:0,ease:Sine.easeOut,onComplete:()=>{
			// 	this._baseContainer.removeChild(this._curScreen);
			// 	this.curScreen=this.prevScreen;
			// 	this.prevScreen=null;
			// }});
		}
		//转换场景
		public changeScreen(toScreenName:string,dispose:boolean=true,paramArr=null):void
		{
			let toScreen=this.getScreenByName(toScreenName,paramArr);
			this.shelter.alpha=0;
			utils.CoreData.instance.stage.addChild(this.shelter);
			// TweenMax.to(this.shelter,.2,{alpha:1,ease:Cubic.easeIn,onComplete:()=>{
			// 	this._baseContainer.removeChild(this._curScreen);
			// 	if(dispose){
			// 		this._curScreen.dispose();
			// 	}else{
			// 		this.prevScreen=this._curScreen;
			// 	}
			// 	this.curScreen=toScreen;
			// 	this._baseContainer.addChild(this._curScreen);
			// 	TweenMax.to(this.shelter,.65,{alpha:0,ease:Sine.easeOut,onComplete:()=>{
			// 		this.shelter.parent.removeChild(this.shelter);
			// 	}});
			// }});
			
			egret.Tween.get(this.shelter).to({'alpha':1},200).call(function(){
				this._baseContainer.removeChild(this._curScreen);
				if(dispose){
					this._curScreen.dispose();
				}else{
					this.prevScreen=this._curScreen;
				}
				this.curScreen=toScreen;
				this._baseContainer.addChild(this._curScreen);

				egret.Tween.get(this.shelter).to({'alpha':0},650).call(function(){
					this.shelter.parent.removeChild(this.shelter);
				}, this);
			},this);

			// egret.Tween.get(this.logo).call(this.tweenComplete, this, ); 
			// .call(this.tweenComplete, this, )

		}
		//谈出回退场景
		public fadeOutBackScreen():void
		{
			if(!this.prevScreen) return;
			this._baseContainer.addChild(this.prevScreen);
			this._baseContainer.addChild(this._curScreen);
			// TweenMax.to(this._curScreen,.4,{alpha:0,ease:Sine.easeOut,onComplete:()=>{
			// 	this._baseContainer.removeChild(this._curScreen);
			// 	this._curScreen.dispose();
			// 	this.curScreen=this.prevScreen;
			// 	this.prevScreen=null;
			// }});
		}

		public set curScreen(value:BaseScreen){
			this._curScreen=value;
		}

		private getScreenByName(screenName:string,paramArr):BaseScreen
		{
			var ScreenClass:any=egret.getDefinitionByName(screenName);
			var screen:BaseScreen;
			if(paramArr) screen=<BaseScreen>(new ScreenClass(...paramArr));
			else screen=<BaseScreen>(new ScreenClass());
			return screen;
		}


	}
}